package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yun.blog.bean.Blog;
import com.yun.blog.bean.Result;
import com.yun.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * title        : SearchController
 * projectName  : dantouBlog-back
 * description  : 搜索功能
 * author       : simple
 * email        : yun_simple@163.com
 * date         : 2023/2/8下午4:58
 */

@Slf4j
@RestController
@RequestMapping("/search")
public class SearchController {

    @Resource
    BlogService blogService;

    @GetMapping("/globalSearch")
    public Result globalSearch(String searchText) {

        try {
            Result success = Result.success("success");
            LambdaQueryWrapper<Blog> queryWrapper = Wrappers.lambdaQuery(Blog.class);
            // 搜索title 中包含关键字的文章，创建时间倒序排列
            queryWrapper.like(Blog::getBlogTitle, searchText).orderByDesc(Blog::getCreateTime);
            List<Blog> blogs = blogService.list(queryWrapper);
            return success.add("blogs", blogs);
        } catch (Exception e) {
            return Result.fail("查询失败！");
        }
    }

}
